﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace kb_24_the_game
{
    public class Level1
    {
        #region Variabelen Declaractie

        public bool BAM = false;

        public string backgroundNaam { get; set; }

        private ContentManager content;
        private Backgrounds scrollBackground1;

        //private Backgrounds scrollBackground2;
        private Player speler;

        private Obstakel trapRechts;
        private Obstakel trapLinks;
        private Obstakel put;
        private Obstakel kegel1, kegel2, kegel3, kegel4, kegel5, kegel6, kegel7;
        private Biertje biertje;
        //private EindItem wallet;
        private Obstakel wallet;
        private ObstakelBal ball;
        private IList<ISprite> allSprites;
        //private IList<ISprite> backgroundsList;
        private List<Obstakel> obstakellijst = new List<Obstakel>();
        private List<Obstakel> trapLijst = new List<Obstakel>();
        private List<Obstakel> kegelLijst = new List<Obstakel>();
        private List<Obstakel> bowlingbalLijst = new List<Obstakel>();
        private SpriteFont font;
        private int score = 0;
        private bool blockLinks = false;
        private bool blockRechts = false;
        private bool bovencollision = false, zijcollisionn = false;

        #endregion Variabelen Declaractie

        public Level1(ContentManager content)
        {
            this.content = content;
            allSprites = new List<ISprite>();
            //backgroundsList = new List<ISprite>();
            speler = new Player(content, new Vector2(51, 425));
            trapLinks = new Obstakel(content, 1, new Vector2(150, 467));
            trapRechts = new Obstakel(content, 2, new Vector2(1000, 467));
            put = new Obstakel(content, 3, new Vector2(1500, 513));
            kegel1 = new Obstakel(content, 4, new Vector2(2400, 0), 250);
            kegel2 = new Obstakel(content, 4, new Vector2(2450, 0), 200);
            kegel3 = new Obstakel(content, 4, new Vector2(2500, 0), 100);
            kegel4 = new Obstakel(content, 4, new Vector2(2550, 0), 125);
            kegel5 = new Obstakel(content, 4, new Vector2(2600, 0), 75);
            kegel6 = new Obstakel(content, 4, new Vector2(2650, 0), 180);
            kegel7 = new Obstakel(content, 4, new Vector2(2700, 0), 60);
            ball = new ObstakelBal(content, new Vector2(700, 470));
            wallet = new Obstakel(content, 6, new Vector2(1900, 1900));
            biertje = new Biertje(content, new Vector2(150, 400), 1);
            font = content.Load<SpriteFont>("AlgemeneTextures/tussenSchermTekst");
            Load();
        }

        public void Load()
        {
            scrollBackground1 = new Backgrounds(content.Load<Texture2D>("Achtergronden/BackgroundLevel01"), new Rectangle(0, 0, 3800, 600));
            //scrollBackground2 = new Backgrounds(content.Load<Texture2D>("Achtergronden/Background02"), new Rectangle(1900, 0, 1900, 600));
            //
            //backgroundsList.Add(scrollBackground1);
            //backgroundsList.Add(scrollBackground2);
            //
            allSprites.Add(scrollBackground1);
            //allSprites.Add(scrollBackground2);
            allSprites.Add(trapLinks);
            allSprites.Add(trapRechts);
            allSprites.Add(kegel1);
            allSprites.Add(kegel2);
            allSprites.Add(kegel3);
            allSprites.Add(kegel4);
            allSprites.Add(kegel5);
            allSprites.Add(kegel6);
            allSprites.Add(kegel7);
            allSprites.Add(put);
            allSprites.Add(wallet);
            allSprites.Add(speler);
            allSprites.Add(ball);
            allSprites.Add(biertje);
            //
            trapLijst.Add(trapRechts);
            trapLijst.Add(trapLinks);
            kegelLijst.Add(kegel1);
            kegelLijst.Add(kegel2);
            kegelLijst.Add(kegel3);
            kegelLijst.Add(kegel4);
            kegelLijst.Add(kegel5);
            kegelLijst.Add(kegel6);
            kegelLijst.Add(kegel7);
            obstakellijst.Add(trapRechts);
            obstakellijst.Add(trapLinks);
            obstakellijst.Add(put);
            obstakellijst.Add(kegel1);
            obstakellijst.Add(kegel2);
            obstakellijst.Add(kegel3);
            obstakellijst.Add(kegel4);
            obstakellijst.Add(kegel5);
            obstakellijst.Add(kegel6);
            obstakellijst.Add(kegel7);
            obstakellijst.Add(wallet);
        }

        public bool Update(GameTime gameTime)
        {
            Rectangle spelerRect = new Rectangle((int)speler.playerPosition.X, (int)speler.playerPosition.Y, speler.texture.Width / 4, speler.texture.Height / 2);
            CheckKeys();
            CheckCollisionsMetTrap(spelerRect);
            CheckColisionBowlingbalTrap();
            CheckCollsionsSpelerBowlingbal(spelerRect);
            CheckCollisionsMetBiertjes(spelerRect);
            CheckCollisionMetPut(spelerRect);
            CheckCollisionTrapMetBiertje();
            CheckBam();
            CheckCollsionsSpelerKegel(spelerRect);
            ball.SetSpelerDirectie(speler.loopDirectie);
            biertje.CheckPositionsSpelerBiertje(speler.playerPosition, score);
            ball.Update(gameTime);
            speler.Update(gameTime);
            foreach (Obstakel k in kegelLijst)
            {
                k.ValKegel(scrollBackground1.rectangle.X);
            }
            if (score >= 5)
            {
                wallet.position = new Vector2(scrollBackground1.rectangle.X + 3400, 470);
            }
            else
            {
                wallet.position = new Vector2(scrollBackground1.rectangle.X + 3400, -500);
            }
            bool einde = CheckEindItem();
            

            foreach (Obstakel obstakel in obstakellijst)
            {
                obstakel.Update(gameTime);
            }
            return einde;

        }

        public void CheckBam()
        {
            if (speler.playerPosition.Y >= 600)
            {
                BAM = true;
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (ISprite sprite in allSprites)
            {
                sprite.Draw(spriteBatch);
            }
            spriteBatch.DrawString(font, "Score: " + score, new Vector2(650, 25), Color.White);
        }

        private void CheckKeys()
        {
            KeyboardState state = Keyboard.GetState();
            if (state.IsKeyDown(Keys.Left) && !blockRechts)
            {
                if (scrollBackground1.rectangle.X <= -1)
                {
                    scrollBackground1.Links();
                    biertje.beweegLinks();
                    foreach (Obstakel obstakel in obstakellijst)
                    {
                        obstakel.beweegLinks();
                    }
                }
                if (speler.playerPosition.X >= 50)
                {
                    speler.Links();
                }
            }
            if (state.IsKeyDown(Keys.Right) && !blockLinks)
            {
                if (scrollBackground1.rectangle.X >= -2999)
                {
                    scrollBackground1.Rechts();
                    biertje.beweegRechts();
                    foreach (Obstakel obstakel in obstakellijst)
                    {
                        obstakel.beweegRechts();
                    }
                }
                if (speler.playerPosition.X <= 500)
                {
                    speler.Rechts();
                }
            }
        }

        public bool CheckEindItem()
        {
            Rectangle portemonee = new Rectangle((int)wallet.position.X, (int)wallet.position.Y, wallet.texture.Width, wallet.texture.Height);
            Rectangle spelerRect = new Rectangle((int)speler.playerPosition.X, (int)speler.playerPosition.Y, 50, 92);
            Rectangle overlap = Rectangle.Intersect(spelerRect, portemonee);
            if (!overlap.IsEmpty)
            {
                return true;
            }
            return false;
        }

        private void CheckCollisionsMetBiertjes(Rectangle spelerRect)
        {
            Rectangle bier = new Rectangle((int)biertje.position.X, (int)biertje.position.Y, biertje.texture.Width, biertje.texture.Height);
            Rectangle overlap = Rectangle.Intersect(spelerRect, bier);
            if (overlap.IsEmpty == false)
            {
                score++;
                biertje.Geraakt(scrollBackground1.rectangle.X, 3500);
            }
        }

        private void CheckCollisionTrapMetBiertje()
        {
            Rectangle bier = new Rectangle((int)biertje.position.X, (int)biertje.position.Y, biertje.texture.Width, biertje.texture.Height);
            foreach (Obstakel o in trapLijst)
            {
                foreach (Rectangle r in o.colliderRect)
                {
                    Rectangle overlap = Rectangle.Intersect(r, bier);
                    if (!overlap.IsEmpty)
                    {
                        biertje.Geraakt(scrollBackground1.rectangle.X, 3500);
                    }
                }
            }
        }

        public void CheckColisionBowlingbalTrap()
        {
            Rectangle bowlingball = new Rectangle((int)ball.ballPosition.X, (int)ball.ballPosition.Y, ball.texture.Width / 8, ball.texture.Height / 2);
            foreach (Obstakel o in trapLijst)
            {
                foreach (Rectangle r in o.colliderRect)
                {
                    Rectangle overlap = Rectangle.Intersect(r, bowlingball);
                    if (!overlap.IsEmpty)
                    {
                        ball.ballbounce = true;
                        return;
                    }
                    else
                    {
                        ball.ballbounce = false;
                    }
                }
            }
        }

        public void CheckCollsionsSpelerBowlingbal(Rectangle spelerRect)
        {
            Rectangle bowlingball = new Rectangle((int)ball.ballPosition.X, (int)ball.ballPosition.Y, ball.texture.Width / 8, ball.texture.Height / 2);
            Rectangle collision = Rectangle.Intersect(spelerRect, bowlingball);
            if (!collision.IsEmpty)
            {
                score = 0;
            }
            return;
        }

        public void CheckCollisionMetPut(Rectangle spelerrect)
        {
            foreach (Rectangle r in put.colliderRect)
            {
                Rectangle overlap = Rectangle.Intersect(spelerrect, r);
                if (!overlap.IsEmpty)
                {
                    if (overlap.Width >= spelerrect.Width)
                    {
                        speler.grondhoogte = 600;
                    }
                }
            }
        }

        public void CheckCollisionsMetTrap(Rectangle spelerRect)
        {
            bovencollision = false;
            zijcollisionn = false;

            foreach (Obstakel o in trapLijst)
            {
                foreach (Rectangle r in o.colliderRect)
                {
                    Rectangle overlap = Rectangle.Intersect(spelerRect, r);

                    if (!overlap.IsEmpty && overlap.Width > overlap.Height && overlap.Width > 8)
                    {
                        //collision
                        speler.grondhoogte = (int)speler.playerPosition.Y;
                        bovencollision = true;
                        continue;
                    }
                    else
                    {
                        if (!bovencollision)
                        {
                            speler.grondhoogte = 425;
                        }
                    }
                    if (!overlap.IsEmpty && overlap.Height > overlap.Width)
                    {
                        if (spelerRect.X < r.X)
                        {
                            //linker kant
                            speler.magRechts = false;
                            speler.magLinks = true;
                            blockLinks = true;
                            blockRechts = false;
                            zijcollisionn = true;
                        }
                        else if (spelerRect.X > r.X)
                        {
                            //rechterkant
                            speler.magLinks = false;
                            speler.magRechts = true;
                            blockLinks = false;
                            blockRechts = true;
                            zijcollisionn = true;
                        }
                    }
                    else if (overlap.IsEmpty && zijcollisionn == false && bovencollision == false)
                    {
                        speler.magLinks = true;
                        speler.magRechts = true;
                        blockLinks = false;
                        blockRechts = false;
                    }
                }
            }
        }

        public void CheckCollsionsSpelerKegel(Rectangle spelerRect)
        {
            foreach (Obstakel k in kegelLijst)
            {
                foreach (Rectangle r in k.colliderRect)
                {
                    Rectangle overlap = Rectangle.Intersect(r, spelerRect);
                    if (!overlap.IsEmpty)
                    {
                        score = 0;
                        k.ResetKegel(scrollBackground1.rectangle.X);
                        return;
                    }
                }
            }
        }
    }
}